package com.ceres.crops.controller;


import com.ceres.crops.pojo.Crops;
import com.ceres.crops.service.CropsService;
import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.github.pagehelper.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/crops")
@SuppressWarnings("all")
public class CropsController {

    @Autowired
    private CropsService cropsService;

    //农作物查询所有功能
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Crops> cropsList = cropsService.findAll();
            return new Result(true,2000,cropsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    //农作物更新功能
    @PutMapping("/update/{id}")
    public Result update(@RequestBody Map map,@PathVariable Integer id){
        try {
            cropsService.update(map,id);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    //农作物根据id查询功能
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        try {
            Crops crops = cropsService.findById(id);
            return new Result(true,2000,crops);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }
    //农作物删除功能(下线)
    @DeleteMapping("/del/{name}")
    public Result del(@PathVariable String name){
        try {
            if (StringUtils.isNotEmpty(name)){
                cropsService.del(name);
            }
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    //农作物添加功能
    @PostMapping("/add")
    public Result add(@RequestBody Map map){
        try {
            cropsService.add(map);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }


    //农作物的分页查询功能
    @GetMapping("/findPage")
    public Result findPage(@RequestParam Map searchMap, String pageNum, String pageSize) {
        try {

            Integer page=1;
            Integer size=5;

            if (StringUtils.isNotEmpty(pageNum) && StringUtils.isNotEmpty(pageSize)){
                try {
                    page=Integer.parseInt(pageNum);
                    size=Integer.parseInt(pageSize);
                } catch (NumberFormatException e) {

                }
            }
            Page<Crops> cropsList = cropsService.pageQuery(searchMap,page,size);

            PageResult pageResult=new PageResult(cropsList.getTotal(),cropsList.getResult());

            return new Result(true,2000,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

}
